<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server
%>
<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*"%>
<%@ include file="Connections/conDB.jsp" %>
<% int validaSession = 2; %>
<%@ include file="Connections/session.jsp" %>

<%
String parId_lote = request.getParameter("motLoteId");
String parCantidad = request.getParameter("cantidad");
//out.print(parId_lote+" "+parCantidad+"<br>");
String parModalidad = request.getParameter("modalidad");
String parId_unidad = request.getParameter("id_unidad");
String parId_loteAnt = request.getParameter("id_loteAnt");

Driver DriverRs = (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
Connection ConnRs = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
String query ="";
PreparedStatement StatementRs =null;
ResultSet Rsquery = null;

if (parModalidad!=null && parModalidad.equals("nuevo") && parId_unidad!=null && !parId_unidad.equals("") && parId_lote!=null && !parId_lote.equals("")) {
	query = "SELECT lote, unidad FROM lote_unidad WHERE lote = "+parId_lote+" AND unidad = "+parId_unidad;
	StatementRs=ConnRs.prepareStatement(query);
	Rsquery=StatementRs.executeQuery();
	if (Rsquery.next()) {
		out.print("<script>");
		out.print("alert('El Motivo/Lote que desea insertar ya existe, por favor, inserte un Motivo/Lote distinto');");
		out.print("history.go(-1);");
		out.print("</script>");
	} else {

		//--- actualiza lote-unidad
		query = "INSERT INTO lote_unidad (lote, unidad, peso) VALUES "+
				"("+parId_lote+","+parId_unidad+","+parCantidad+")";
		//out.print(query);
		StatementRs = ConnRs.prepareStatement(query);
	   	StatementRs.executeUpdate();
		
		// --- actualiza el peso de la unidad
		query = "UPDATE unidad SET "+
				"peso_origen = "+
				"(SELECT SUM(peso) as peso FROM lote_unidad WHERE unidad = "+parId_unidad+") "+
				"WHERE id_unidad = "+parId_unidad;
		//out.print(query);
		StatementRs = ConnRs.prepareStatement(query);
	   	StatementRs.executeUpdate();

	}
	Rsquery.close();

} //if (parModalidad!=null && parModalidad.equals("nuevo"))

if (parModalidad!=null && parModalidad.equals("modificar") && parId_unidad!=null && !parId_unidad.equals("") && parId_lote!=null && parId_loteAnt!=null) {
	query = "SELECT lote, unidad FROM lote_unidad WHERE lote = "+parId_lote+" AND unidad = "+parId_unidad;
	StatementRs=ConnRs.prepareStatement(query);
	Rsquery=StatementRs.executeQuery();
	if (Rsquery.next() && !parId_loteAnt.equals(parId_lote)) {
		out.print("<script>");
		out.print("alert('El Motivo/Lote que desea insertar ya existe, por favor, inserte un Motivo/Lote distinto');");
		out.print("history.go(-1);");
		out.print("</script>");
	} else {

		//--- actualiza lote-unidad
		query = "UPDATE lote_unidad SET "+
				"lote = "+parId_lote+", "+
				"unidad = "+parId_unidad+", "+
				"peso = "+parCantidad+" "+
				"WHERE lote = "+parId_loteAnt+" AND unidad = "+parId_unidad;
		//out.print(query);
		StatementRs = ConnRs.prepareStatement(query);
	   	StatementRs.executeUpdate();

		// --- actualiza el peso de la unidad
		query = "UPDATE unidad SET "+
				"peso_origen = "+
				"(SELECT SUM(peso) as peso FROM lote_unidad WHERE unidad = "+parId_unidad+") "+
				"WHERE id_unidad = "+parId_unidad;
		//out.print(query);
		StatementRs = ConnRs.prepareStatement(query);
	   	StatementRs.executeUpdate();

	}
	Rsquery.close();

} //if (parModalidad!=null && parModalidad.equals("modificar"))

	out.print("<script>");
	out.print("window.opener.location.reload();");
	out.print("window.close();");
	out.print("</script>");


ConnRs.close();
%>
<html>
<head>
<title>CIMA v2 - Guardar Motivo/Lote Unidad</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">

</body>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>

